package top.luckyluxcis.controller;

import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.luckyluxcis.vo.MessageVo;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Luxcis
 * @since 2022/8/8
 */
@RestController()
public class TestController {
    @Resource
    private StreamBridge streamBridge;

    @GetMapping("/mq")
    public ResponseEntity<String> mq() {
        streamBridge.send("testConsumer1-out-0", MessageBuilder.withPayload(new MessageVo("out-1", System.currentTimeMillis())).build());
        // 延时队列(RabbitMQ需要安装插件：rabbitmq_delayed_message_exchange)
        Map<String,Object> headers = new HashMap<>(1);
        // 设置延时时间（单位：ms）
        headers.put("x-delay", 10 * 1000);
        streamBridge.send("testConsumer2-out-0", MessageBuilder.withPayload(new MessageVo("out-2-delay", System.currentTimeMillis())).copyHeaders(headers).build());
        return ResponseEntity.ok("MQ");
    }
}
